package com.example.wechat.csdnandroid.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;

import com.example.wechat.csdnandroid.R;
import com.example.wechat.csdnandroid.service.MyService;

public class StartStopSeriviceActivity extends AppCompatActivity {

    private Intent intent;
    ServiceConnection conn;
    private static final String TAG = "StartStopSeriviceActivi";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_stop_serivice);
        intent = new Intent(this, MyService.class);

        conn=new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.e(TAG, "onServiceConnected: " +name);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.e(TAG, "onServiceDisconnected: "+name );
            }
        };
    }

    public void startService(View view) {
        startService(intent);
    }

    public void stopService(View view) {
        stopService(intent);
    }

    public void bindService(View view) {
        bindService(intent,conn, Service.BIND_AUTO_CREATE);
    }

    public void unbindService(View view) {
       if(conn!=null){
           unbindService(conn);
       }
    }
}
